package spynx.net.custom;

import java.net.URL;

import spynx.desl.Statement;
import spynx.desl.runtime.IExecutionContext;
import spynx.desl.runtime.IPatternMatchInfo;
import spynx.desl.runtime.matchinfos.impl.PatternMatchInfo;
import spynx.net.core.UrlCacheResolver;
import spynx.util.DeslRuntimeUtil;

public class DeslUrlNameResolver extends UrlCacheResolver {
	private Statement statement;
	private IExecutionContext context; 
	
	public DeslUrlNameResolver(IExecutionContext context, Statement statement) {
		this.context = context;
		this.statement = statement;
	}
	

	@Override
	public String resolveCacheName(URL query) {
		Object resolvedName = null;
		PatternMatchInfo matchInfo = new PatternMatchInfo();
		IExecutionContext childContext = context.createChildContext();
		DeslRuntimeUtil.setMatchInfo(childContext, matchInfo);
		
		matchInfo.setInput(query.toString());
		
		try {
			resolvedName = statement.execute(childContext);
			if (resolvedName instanceof IPatternMatchInfo)
				resolvedName = ((IPatternMatchInfo) resolvedName).getReturnedResult();
		}
		catch (RuntimeException ex) {
			;
		}
		return resolvedName != null ? resolvedName.toString() : null;
	}
}
